package com.lora.judge;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.lora.judge.model.CheckItem;
import com.lora.judge.type.*;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

public class CheckUtil {

    public static CheckModel getCheck(String checkModel) {
        if("GT".equals(checkModel)) {
            return GtCheck.getInstance();
        } else if("GTE".equals(checkModel)) {
            return GteCheck.getInstance();
        } else if("LT".equals(checkModel)) {
            return LtCheck.getInstance();
        } else if("LTE".equals(checkModel)) {
            return LteCheck.getInstance();
        } else if("EQ".equals(checkModel)) {
            return EqCheck.getInstance();
        } else if("RANGE".equals(checkModel)) {
            return RangeCheck.getInstance();
        }
        return null;
    }

    public static String alarm(Object equipment, String alarmConfig) throws IllegalAccessException {
        List<String> alarmGroups = Lists.newArrayList();
        JSONObject object = JSONObject.parseObject(alarmConfig);
        String field = object.getString("field");
        String epName = object.getString("epName");
        String title = object.getString("title");
        String type = object.getString("valueType");
        String customTip = object.getString("customTip");
        JSONArray array = object.getJSONArray("checks");
        Field[] declaredFields = equipment.getClass().getDeclaredFields();
        if("BigDecimal".equals(type)) {
            CheckItem<BigDecimal> item = new CheckItem<>();
            item.setCustomTip(customTip);
            item.setEquipmentName(epName);
            item.setFiledName(field);
            item.setFiledTitle(title);
            Field field1 = Arrays.asList(declaredFields).stream().filter(v -> v.getName().equals(field)).findFirst().orElse(null);
            if(field1 != null) {
                for(Object obj : array) {
                    field1.setAccessible(true);
                    Object o = field1.get(equipment);
                    if(null == o) {
                        continue;
                    }
                    JSONObject object1 = JSONObject.parseObject(obj.toString());
                    item.setCheckObject(new BigDecimal(o+""));
                    item.setCheckAlgorithm(object1.getString("algorithm"));
                    if(StringUtils.isNotBlank(object1.getString("standard"))) {
                        item.setStandard(new BigDecimal(object1.getString("standard")));
                    }
                    if(StringUtils.isNotBlank(object1.getString("begin"))) {
                        item.setBegin(new BigDecimal(object1.getString("begin")));
                    }
                    if(StringUtils.isNotBlank(object1.getString("end"))) {
                        item.setEnd(new BigDecimal(object1.getString("end")));
                    }
                    String alarm = item.check();
                    if(StringUtils.isNotBlank(alarm)) {
                        alarmGroups.add(alarm);
                    }
                }
            }
        } else if("String".equals(type)) {
            CheckItem<String> item = new CheckItem<>();
            item.setCustomTip(customTip);
            item.setEquipmentName(epName);
            item.setFiledName(field);
            item.setFiledTitle(title);
            Field field1 = Arrays.asList(declaredFields).stream().filter(v -> v.getName().equals(field)).findFirst().orElse(null);
            if(field1 != null) {
                for(Object obj : array) {
                    field1.setAccessible(true);
                    Object o = field1.get(equipment);
                    if(null == o) {
                        continue;
                    }
                    JSONObject object1 = JSONObject.parseObject(obj.toString());
                    item.setCheckObject((String) o);
                    item.setStandard(object1.getString("standard"));
                    item.setCheckAlgorithm(object1.getString("algorithm"));
                    item.setBegin(object1.getString("begin"));
                    item.setEnd(object1.getString("end"));
                    String alarm = item.check();
                    if(StringUtils.isNotBlank(alarm)) {
                        alarmGroups.add(alarm);
                    }
                }
            }
        }
        return StringUtils.join(alarmGroups, ";");
    }

}
